
//$(".form-horizontal").validationEngine();//验证表单
//打开增加既往史框
function addPre(t){
	$.ajax({ 
		type : "get",
		dataType: "html",
		url : "../history/addHealthHistoryPre",
		async: true,
		data:{
			historyType:$(t).attr("data-type"),
			memberId:$(t).attr("data-memberId")
		},
		success : function(data) {
			$("#healthHistory_ill_dialog").empty().html(data);
			deleteM(t,null);
		}
	});
}
//打开修改既往史框
function updatePre(t){
	$.ajax({ 
		type : "get",
		dataType: "html",
		url : "../history/updateHealthHistoryPre",
		async: true,
		data:{
			historyType:$(t).attr("data-type"),
			healthHistoryId:$(t).attr("data-id"),
			sn:$(t).attr("data-sn")
			//lastUpdateNo:$(t).attr("data-lastUpdateNo")
				
		},
		success : function(data) {
			$("#healthHistory_ill_dialog").empty().html(data);
			deleteM(t,null);
		}
	});
}
//删除
function removeHistory(t){
	var iconFirm=window.confirm("确定删除这条记录？");
	var sn=$(t).attr("data-sn");
	var historyType=$(t).attr("data-type");
	var dataSize
	var trId;
	
	var healthHistoryId=$(t).attr("data-id");
	var lastUpdateNo=$(t).attr("data-lastUpdateNo");
	
	if(historyType==0){
		var sizeOfList=$("#sizeOfListIll").val();
	}else if(historyType==1){
		var sizeOfList=$("#sizeOfListSurgery").val();
	}else if(historyType==2){
		var sizeOfList=$("#sizeOfListIngury").val();
	}else if(historyType==3){
		var sizeOfList=$("#sizeOfListTransBlood").val();
	}
	
	if(iconFirm==true){
		$.ajax({
			type : "post",
			dataType: "json",
			url : "../history/deleteHealthHistory",
			async: true,
			data:{
				healthHistoryId:healthHistoryId,
				lastUpdateNo:lastUpdateNo
			},
			success : function(data) {
				if(data.isSuccess){
					sizeOfList=(eval(sizeOfList)-1);
					if(historyType==0){
						
						$("#sizeOfListIll").val(sizeOfList);
					}else if(historyType==1){
						
						$("#sizeOfListSurgery").val(sizeOfList);
					}else if(historyType==2){
			
						$("#sizeOfListIngury").val(sizeOfList);
					}else if(historyType==3){
		
						$("#sizeOfListTransBlood").val(sizeOfList);
					}
					
					removerTrx(t,historyType);
					hidePop();
					
				}else{
					zhyAlert("删除失败");
					hidePop();
				}
			}
		});
		
		
		
	}
	
	
}

function removerTrx(obj,historyType){
	obj =$(obj);
	var index =obj.parents("tr").find("td").eq(0);
	var next_tr=obj.parents("tr").nextAll();
	var trLenth=obj.parents("tbody").find("tr").length;
	if(trLenth==1){
		//0疾病；1手术；2外伤；3输血
		if(historyType==0){
			obj.parents("table").append("<tr id='NoDataIll'><td style='text-align: center' colspan='5'>暂无数据</td></tr>");
		}else if(historyType==1){
			obj.parents("table").append("<tr id='NoDataSugery'><td style='text-align: center' colspan='5'>暂无数据</td></tr>");
		}else if(historyType==2){
			obj.parents("table").append("<tr id='NoDataIngury'><td style='text-align: center' colspan='5'>暂无数据</td></tr>");
		}else if(historyType==3){
			obj.parents("table").append("<tr id='NoDataTransBlood'><td style='text-align: center' colspan='5'>暂无数据</td></tr>");
		}
		
	}
	next_tr.each(function(){
		var td=$(this).find("td").eq(0);
		td.text(td.text()-1);
	})
	obj.parents("tr").remove();
}
//增加既往史 动作
function addHistory(){
	var memberId=$("#memberId").val();
	var confimTime=$("#confirmTime").val();
	var historyType=$("#historyType").val();
	var diseaseName=$("#diseaseName").val();
	var remark=$("#remarkHistory").val();
	
	if($.trim(diseaseName).length > 16){
		zhyAlert("名称应小于16个字符！");
		return;
	}
	if($.trim(diseaseName).length == 0 || diseaseName==''){
		if($.trim(historyType).length == 0){
			zhyAlert("请填写疾病");
		}else{
			zhyAlert("请填写名称");
		}
		return;
	}
	if(!validateDate(confimTime)){
		zhyAlert("时间格式错误！");
		return;
	}
	if(!interceptIllegal(diseaseName)){
		zhyAlert("名称含有非法字符");
		return;
	}
	if(!interceptIllegal(remark)){
		zhyAlert("备注含有非法字符");
		return;
	}
	//拦截非法字符
	
	////0疾病；1手术；2外伤；3输血
	if(historyType==0){
		var sizeOfList=$("#sizeOfListIll").val();
	}else if(historyType==1){
		var sizeOfList=$("#sizeOfListSurgery").val();
	}else if(historyType==2){
		var sizeOfList=$("#sizeOfListIngury").val();
	}else if(historyType==3){
		var sizeOfList=$("#sizeOfListTransBlood").val();
	}
	
	$.ajax({
		type : "post",
		dataType: "json",
		url : "../history/addHealthHistory",
		async: true,
		data:{
			memberId:memberId,
			confimTime:confimTime,
			historyType:historyType,
			diseaseName:diseaseName,
			remark:remark,
		},
		success : function(data) {
			if(data.isSuccess){
				var healthHistoryId=data.healthHistoryId;
				sizeOfList=(eval(sizeOfList)+1);
				var heal_edit1 = data.heal_edit;//修改标示
				var heal_delete1 = data.heal_delete;//删除标示
				addRow(sizeOfList,diseaseName,confimTime,remark,historyType,healthHistoryId,heal_edit1,heal_delete1);
				hidePop();
				$("#healthHistory_ill_dialog").empty();
			}else{
				zhyAlert("保存失败");
				hidePop();
			}
		}
	});
}

//执行修改动作
function updateHistory(){
	var memberId=$("#memberId").val();
	var healthHistoryId=$("#healthHistoryId").val();
	var confimTime=$("#confirmTime").val();
	var historyType=$("#historyType").val();
	var diseaseName=$("#diseaseName").val();
	var remark=$("#remarkHistory").val();
	var lastUpdateNo=$("#lastUpdateNo").val();
	var formData=$("#updateIllForm").serialize();
	
	var sn=$("#updateSn").val();//序号
	var trId;
	if($.trim(diseaseName).length > 16){
		zhyAlert("名称应小于16个字符！");
		return;
	}
	if($.trim(diseaseName).length == 0 || diseaseName==''){
		if($.trim(historyType).length == 0){
			zhyAlert("请填写疾病");
		}else{
			zhyAlert("请填写名称");
		}
		return;
	}
	if(!validateDate(confimTime)){
		zhyAlert("时间格式错误！");
		return;
	}
	if(!interceptIllegal(diseaseName)){
		zhyAlert("名称含有非法字符");
		return;
	}
	if(!interceptIllegal(remark)){
		zhyAlert("备注含有非法字符");
		return;
	}
	//0疾病；1手术；2外伤；3输血
	if(historyType==0){
		trId='healthIll_'+sn;
	}else if(historyType==1){
		trId='healthSurgery_'+sn;
	}else if(historyType==2){
		trId='healthHistoryINjury_'+sn;
	}else if(historyType==3){
		trId='healthBloodTrans_'+sn;
	}
	$.ajax({
		type : "post",
		dataType: "json",
		url : "../history/updateHealthHistory",
		async: true,
		data:{
			healthHistoryId:healthHistoryId,
			diseaseName:diseaseName,
			confimTime:confimTime,
			remark:remark,
			lastUpdateNo:lastUpdateNo
		},
		success : function(data) {
			if(data.isSuccess){
				var heal_edit2 = data.heal_edit;//修改标示
				var heal_delete2 = data.heal_delete;//删除标示
				lastUpdateNo=eval(lastUpdateNo)+1;
				updateSuccess(trId,sn,diseaseName,confimTime,remark,historyType,healthHistoryId,lastUpdateNo,heal_edit2,heal_delete2)
				hidePop();
				$("#healthHistory_ill_dialog").empty();
			}else{
				zhyAlert("保存失败");
				hidePop();
			}
		}
	});
}



//修改成功调用
function updateSuccess(trId,sn,diseaseName,confimTime,remark,historyType,healthHistoryId,lastUpdateNo,heal_edit,heal_delete){
	var updateTr=$("#"+trId);
//	var edit_str2 = "";
//	if(heal_edit){
//		edit_str2 ="<span class='colorblue edit'><a data-type='"+historyType+"' " +
//		"data-id='"+healthHistoryId+"' data-sn='"+sn+"' " +
//		"data-lastUpdateNo='"+lastUpdateNo+"' onclick='updatePre(this)' " +
//		"href='javascript:;' class='colorblue'>修改</a></span>";
//	}
//	var del_str2 = "";
//	if(heal_delete){
//		del_str2= "<span data-id='"+healthHistoryId+"' data-lastUpdateNo='"+lastUpdateNo+"' " +
//		"'data-type='"+historyType+"' class='colorred delete' onclick='removeHistory(this)'>删除</span>";
//	}
	var edit_str2 = "";
		edit_str2 ="<span class='colorblue edit'><a data-type='"+historyType+"' " +
						"data-id='"+healthHistoryId+"' data-sn='"+sn+"' " +
						"data-lastUpdateNo='"+lastUpdateNo+"' onclick='updatePre(this)' " +
						"href='javascript:;' class='colorblue'>修改</a></span>";
	var del_str2 = "";
		del_str2= "<span data-id='"+healthHistoryId+"' data-lastUpdateNo='"+lastUpdateNo+"' " +
						"'data-type='"+historyType+"' class='colorred delete' onclick='removeHistory(this)'>删除</span>";
	var newTr="<td>"+sn+"</td>"+
				"<td title='"+diseaseName+"'>"+diseaseName.substring(0,8)+(diseaseName.length>8?"...":"")+"</td>"+
				"<td>"+confimTime+"</td>"+
				"<td title='"+remark+"'>"+remark.substring(0,16)+(remark.length>16?"...":"")+"</td>"+
				"<td>" + edit_str2 + del_str2 + "</td>";
	updateTr.empty();
	updateTr.append(newTr);
	
}
function addRow(sizeOfList,diseaseName,confimTime,remark,historyType,healthHistoryId,heal_edit,heal_delete){
	var iTable;
	var trId;
////0疾病；1手术；2外伤；3输血
	if(historyType==0){
		iTable=$("#illTable");
		trId='healthIll_';
		$("#NoDataIll").remove();
		$("#sizeOfListIll").val(sizeOfList);
	}else if(historyType==1){
		iTable=$("#surgeryTable");
		trId='healthSurgery_';
		$("#NoDataSugery").remove();
		$("#sizeOfListSurgery").val(sizeOfList);
	}else if(historyType==2){
		iTable=$("#injuryTable");
		trId='healthHistoryINjury_';
		$("#NoDataIngury").remove();
		$("#sizeOfListIngury").val(sizeOfList);
	}else if(historyType==3){
		iTable=$("#bloodTransTable"); 
		trId='healthBloodTrans_'
		$("#NoDataTransBlood").remove();
		$("#sizeOfListTransBlood").val(sizeOfList);
	}
//	var edit_str1 = "";
//	if(heal_edit){
//		edit_str1 ="<span class='colorblue edit'><a data-type='"+historyType+"' " +
//		"data-id='"+healthHistoryId+"' data-sn='"+sizeOfList+"' " +
//		"data-lastUpdateNo='1' onclick='updatePre(this)' " +
//		"href='javascript:;' class='colorblue'>修改</a></span>";
//	}
//	var del_str1 = "";
//	if(heal_delete){
//		del_str1 = "<span data-id='"+healthHistoryId+"' data-type='"+historyType+"' data-lastUpdateNo='1' " +
//		"  class='colorred delete' onclick='removeHistory(this)'>删除</span>";
//	}
	var edit_str1 = "";
		edit_str1 ="<span class='colorblue edit'><a data-type='"+historyType+"' " +
			"data-id='"+healthHistoryId+"' data-sn='"+sizeOfList+"' " +
			"data-lastUpdateNo='1' onclick='updatePre(this)' " +
			"href='javascript:;' class='colorblue'>修改</a></span>";
	var del_str1 = "";
		del_str1 = "<span data-id='"+healthHistoryId+"' data-type='"+historyType+"' data-lastUpdateNo='1' " +
			"  class='colorred delete' onclick='removeHistory(this)'>删除</span>";
	iTable.append("<tr id="+trId+sizeOfList+">" +
			"<td>"+sizeOfList+"</td>"+
			"<td title='"+diseaseName+"'>"+diseaseName.substring(0,8)+(diseaseName.length>8?"...":"")+"</td>"+
			"<td>"+confimTime+"</td>"+
			"<td title='"+remark+"'>"+remark.substring(0,16)+(remark.length>16?"...":"")+"</td>"+
			"<td>" +edit_str1 + del_str1 +
			"</td>"+
	  "</tr>");
}



